package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.BeanUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.CategoryMapper;
import com.zhijian.warehouse.manage.entity.po.Category;
import com.zhijian.warehouse.manage.entity.vo.request.CategorySaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.CategorySearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.CategoryVo;
import com.zhijian.warehouse.manage.enums.DelFlagEnum;
import com.zhijian.warehouse.manage.service.CategoryService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 品类业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-10 20:08:21
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper mapper;

    @Override
    public Category selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public CategoryVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        Category category = this.selectById(id);
        AssertCommonUtil.notNull(category, "此数据不存在或者已被删除");
        this.updateByPrimaryKeySelective(Category.builder().id(id)
                .isDelete(DelFlagEnum.DELETED.value).build());
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        for (Long id : idList) {
            this.deleteById(id);
        }
    }

    @Override
    public Long insert(Category category) {
        Date now = new Date();
        category.setCreateTime(now);
        category.setUpdateTime(now);
        return mapper.insert(category) > 0 ? category.getId() : 0L;
    }

    @Override
    public Long insertSelective(Category category) {
        Date now = new Date();
        category.setCreateTime(now);
        category.setUpdateTime(now);
        return mapper.insertSelective(category) > 0 ? category.getId() : 0L;
    }

    @Override
    public boolean update(Category category) {
        Date now = new Date();
        category.setUpdateTime(now);
        return mapper.updateByPrimaryKey(category) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Category record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(CategorySaveOrUpdateVo saveOrUpdateVo) {
        Integer count = this.count(CategorySearchVo.builder()
                .warehouseId(saveOrUpdateVo.getWarehouseId())
                .number(saveOrUpdateVo.getNumber())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "编号重复");
        count = this.count(CategorySearchVo.builder()
                .warehouseId(saveOrUpdateVo.getWarehouseId())
                .isDelete(DelFlagEnum.USING.value)
                .nameAccurate(saveOrUpdateVo.getName())
                .build());
        AssertCommonUtil.check(count > 0, "名称重复");
        Category category = new Category();
        BeanUtils.copyProperties(saveOrUpdateVo, category);
        if (BeanUtil.isEmpty(category.getParentId()))
            category.setParentId(0L);
        boolean b = this.insertSelective(category) != 0;
        saveOrUpdateVo.setId(category.getId());
        return b;
    }

    @Override
    public boolean modify(CategorySaveOrUpdateVo saveOrUpdateVo) {
        Category old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        Integer count = this.count(CategorySearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .warehouseId(saveOrUpdateVo.getWarehouseId())
                .number(saveOrUpdateVo.getNumber())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "编号重复");
        count = this.count(CategorySearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .warehouseId(saveOrUpdateVo.getWarehouseId())
                .isDelete(DelFlagEnum.USING.value)
                .nameAccurate(saveOrUpdateVo.getName())
                .build());
        AssertCommonUtil.check(count > 0, "名称重复");
        old.setNumber(saveOrUpdateVo.getNumber());
        old.setName(saveOrUpdateVo.getName());
        old.setParentId(saveOrUpdateVo.getParentId());
        old.setUnitId(saveOrUpdateVo.getUnitId());
        old.setWarehouseId(saveOrUpdateVo.getWarehouseId());
        old.setLimitDay(saveOrUpdateVo.getLimitDay());
        old.setUserIds(saveOrUpdateVo.getUserIds());
        old.setGroupIds(saveOrUpdateVo.getGroupIds());
        old.setExt1(saveOrUpdateVo.getExt1());
        old.setExt2(saveOrUpdateVo.getExt2());
        old.setNote(saveOrUpdateVo.getNote());
        old.setUpdateUserId(saveOrUpdateVo.getUpdateUserId());
        old.setUpdateUserName(saveOrUpdateVo.getUpdateUserName());
        old.setIsDelete(DelFlagEnum.USING.value);
        return this.update(old);
    }

    @Override
    public CategoryVo selectOne(CategorySearchVo categorySearchVo) {
        return mapper.selectOne(categorySearchVo);
    }

    @Override
    public Integer count(CategorySearchVo categorySearchVo) {
        return mapper.count(categorySearchVo);
    }

    @Override
    public List<CategoryVo> selectList(CategorySearchVo categorySearchVo) {
        return mapper.selectList(categorySearchVo);
    }

    @Override
    public Page<CategoryVo> selectPage(CategorySearchVo categorySearchVo) {
        AssertCommonUtil.check(Objects.isNull(categorySearchVo.getPageNum()) || Objects.isNull(categorySearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(categorySearchVo);
        //分页
        Page<CategoryVo> page = new Page<>(true, count, categorySearchVo.getPageNum(), categorySearchVo.getPageSize());
        categorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<CategoryVo> responseVos = this.selectList(categorySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<Category> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }


    @Override
    public List<CategoryVo> selectAllToTree() {
        List<CategoryVo> categoryVos = this.selectList(CategorySearchVo.builder()
                .isDelete(DelFlagEnum.USING.value)
                .build());
        // 构建树形结构
        return buildTree(categoryVos, 0L);
    }

    private List<CategoryVo> buildTree(List<CategoryVo> categoryVos, Long parentId) {
        List<CategoryVo> result = CollectionUtil.newArrayList();
        for (CategoryVo categoryVo : categoryVos) {
            if (Objects.equals(categoryVo.getParentId(), parentId)) {
                result.add(categoryVo);
                categoryVo.setChildren(buildTree(categoryVos, categoryVo.getId()));
            }
        }
        return result;
    }

}
